home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Frameworks / Extension Shell 1.3 / Extension Shell 1.3 (Source) / NotifyMsg.c < prev    next >
Encoding:
Text File  |  1994-04-06  |  4.8 KB  |  162 lines  |  [TEXT/R*ch]

  1. /*    NAME:
  2.         NotifyMsg.c
  3.  
  4.     WRITTEN BY:
  5.         Joe Holt
  6.                 
  7.     MODIFIED BY:
  8.         Dair Grant
  9.  
  10.     DESCRIPTION:
  11.         When your INIT/cdev runs into a problem, clean things up, show the X'ed
  12.         version of your icon and call NotificationMessage() with the ID of
  13.         the string you want displayed.
  14.  
  15.     NOTES:
  16.         •    Depends on a separate code resource to remove the actual
  17.             notification message from the System Heap.
  18.  
  19.     ___________________________________________________________________________
  20.  
  21.     VERSION HISTORY:
  22.         (June 1990, jhh)
  23.             •    Response to Usenet news posting. Archived in Usenet Macintosh
  24.                 Programmer's Guide.
  25.  
  26.         (Oct 1993, dg)
  27.             •    Altered the params to NotificationMessage to allow specifying of
  28.                 the 'STR#' resource that the error string is pulled from. If no
  29.                 string can be found, no note is posted.
  30.             •    Altered the logic to do with 'RESP' code. If we can't get a 'RESP' code
  31.                 we still post the note, but allow it to stay stuck in the System Heap.
  32.                 The memory hit is negligible compared to the advantage of the user
  33.                 seeing the note.
  34.  
  35.         (Jan 1994, dg)
  36.             •    Changed the type of the 'RESP' code to 'CODE'. Specified in
  37.                 ESConstants.h
  38.  
  39.  
  40.     ___________________________________________________________________________
  41. */
  42. //=============================================================================
  43. //        Include files                                                                     
  44. //-----------------------------------------------------------------------------
  45. #include <Memory.h>
  46. #include <Notification.h>
  47. #include "ESConstants.h"
  48. #include "NotifyMsg.h"
  49.  
  50.  
  51.  
  52.  
  53.  
  54. //=============================================================================
  55. //        Private defines                                                             
  56. //-----------------------------------------------------------------------------
  57. //    T_NMInstall and T_Unimplemented are Mac toolbox trap numbers used to
  58. //    test for the existence of the Notification Manager.
  59. #define T_NMInstall         (0xA05E)
  60. #define T_Unimplemented     (0xA89F)
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. //=============================================================================
  72. //        NotificationMessage : Post a Notification Manager request.                                                                 
  73. //-----------------------------------------------------------------------------
  74. //        Note :    The 'RESP' code is needed to dispose of the NM record we leave
  75. //                in the System Heap. If the 'RESP' code cannot be loaded, the
  76. //                record is stuck until the next reboot. As these records are pretty
  77. //                small - this isn't that important.
  78. //-----------------------------------------------------------------------------
  79. void NotificationMessage(int theStrings, int theStr)
  80. {    register NMRec  *note;
  81.     register Handle responseCode;
  82.     register long   size;
  83.     register THz    svZone;
  84.     Str255          errorText;
  85.  
  86.  
  87.  
  88.  
  89.     // Make sure we've got a Notification Manager.
  90.     if (NGetTrapAddress(T_NMInstall, OSTrap) != NGetTrapAddress(T_Unimplemented, ToolTrap))
  91.         {
  92.         // All of the memory we allocate from here on out is in the System
  93.         // Heap.  First create a Notification Manager record and fill it in.
  94.         svZone    = TheZone;
  95.         TheZone    = SysZone;
  96.         note    = (NMRec *) NewPtr(sizeof(NMRec));
  97.         if (!note)
  98.             goto exit;
  99.         note->qType        = nmType;
  100.         note->nmMark    = 0;
  101.         note->nmIcon    = 0L;
  102.         note->nmSound    = (Handle) -1;
  103.  
  104.  
  105.  
  106.         // Get the error message corresponding to the error number given.
  107.         // For maximum performance, the STR# resource should be tagged
  108.         // "Preload" and not "System Heap".  This way, you can be sure
  109.         // the messages will be there even if memory space is the cause of
  110.         // the error.
  111.         // 
  112.         // We create a pointer in the System Heap just big enough for the
  113.         // string and copy the string into it.  Point the NM record at this
  114.         // string. If the string's length is zero we abort.
  115.         GetIndString(errorText, theStrings, theStr);
  116.         size        = (*(unsigned char *) errorText) + 1;
  117.         note->nmStr    = (StringPtr) NewPtr(size);
  118.         if (!note->nmStr || errorText[0] == 0)
  119.             {
  120.             DisposPtr(note);
  121.             goto exit;
  122.             }
  123.         BlockMove(errorText, note->nmStr, size);
  124.  
  125.  
  126.  
  127.         // The response procedure also must be in a pointer in the System
  128.         // Heap.  You need to include the compiled code resource in your
  129.         // INIT's resources.
  130.         // 
  131.         // Create a pointer just big enough for it and point the NM record
  132.         // at it, also.
  133.         //
  134.         //    dg -    Changed logic so that note still gets posted even if we
  135.         //            couldn't get a 'RESP' procedure to unload it.
  136.         responseCode = GetResource(kRespCodeType, kRespCodeID);
  137.         if (!responseCode)
  138.             note->nmResp = (Ptr) -1;
  139.         else
  140.             {
  141.             size = GetHandleSize(responseCode);
  142.             note->nmResp = (ProcPtr) NewPtrSys(size);
  143.             if (!note->nmResp)
  144.                 {
  145.                 DisposPtr(note->nmStr);
  146.                 DisposPtr(note);
  147.                 goto exit;
  148.                 }
  149.             HLockHi(responseCode);
  150.             BlockMove(*responseCode, note->nmResp, size);
  151.             }
  152.  
  153.  
  154.  
  155.         // Now post the note.  As soon as startup is complete, the NM
  156.         // will display the note for the user's edification.  Hurrah.
  157.         NMInstall(note);
  158. exit:
  159.         TheZone = svZone;
  160.         }
  161. }
  162.